import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/main/login/http_const.dart';
import 'package:share_fit/app/controllers/space/person/del_dialog_confirm.dart';
import 'package:share_fit/app/controllers/space/person/person_logic.dart';
import 'package:share_fit/app/controllers/space/weekly/string_util.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/network/net_const.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

class AccountDeletePage extends StatelessWidget {

  final logic = Get.put(PersonLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        // backgroundColor: Color(0xFFF5F9FB),
        title: Text(LocaleKeys.space_account_del.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
      ),
      body: Container(
        margin: EdgeInsets.fromLTRB(42.w, 96.h, 42.w, 120.h),
        child: Column(
          children: [
            RichText(text: TextSpan(
                children: [
                  TextSpan(text: StringUtils.autoLineString(LocaleKeys.account_del_clear.ts), style: TextStyle(fontSize: 46.sp, color: ColorConst.accentColor),),
                  TextSpan(text: StringUtils.autoLineString(LocaleKeys.account_del_clear_01.ts), style: TextStyle(fontSize: 46.sp, color: ColorConst.secondaryFontColor),),
                  TextSpan(text: StringUtils.autoLineString(LocaleKeys.account_del_clear_02.ts), style: TextStyle(fontSize: 46.sp, color: ColorConst.secondaryFontColor),),
                  TextSpan(text: StringUtils.autoLineString(LocaleKeys.account_del_clear_03.ts), style: TextStyle(fontSize: 46.sp, color: ColorConst.secondaryFontColor),),
                  TextSpan(text: StringUtils.autoLineString(LocaleKeys.account_del_clear_04.ts), style: TextStyle(fontSize: 46.sp, color: ColorConst.secondaryFontColor),),
                ]
            ),),
            Spacer(),
            GestureDetector(
              onTap: (){logic.setBottomChecked(!logic.state.isBottomSelected);},
              child: Container(
                margin: EdgeInsets.fromLTRB(42.w, 88.h, 0, 0.h),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    GetBuilder<PersonLogic>(
                        id: 'del_bottom_selected', builder: (_){
                      return Stack(
                        children: [
                          SizedBox(width: 42.w,),
                          Center(
                            child: Image.asset('assets/images/space/icon_login_unselected.png', width: 47.w, height: 47.h,),
                          ),
                          Opacity(opacity: logic.state.isBottomSelected?1:0, child: Center(
                            child: Image.asset('assets/images/space/icon_login_selected.png', width: 58.w, height: 45.h,),),)
                        ],
                      );
                    }),
                    SizedBox(width: 22.w,),
                    Expanded(child: Text(LocaleKeys.account_del_bottom_tips.ts, style: TextStyle(fontSize: 40.sp, color: ColorConst.secondaryFontColor),),)
                  ],
                ),
              ),
            ),
            Container(
              height: 136.h,
              width: 1.sw,
              margin: EdgeInsets.fromLTRB(0.w, 40.h, 0.w, 0.h),
              child: ElevatedButton(
                onPressed: () {
                  if(!logic.state.isBottomSelected) {
                    logic.showTips('', LocaleKeys.account_del_bottom_checked.ts);
                    return;
                  }
                  logic.save('temp_text', '');
                  showDialog(context: context, builder: (_){
                    return DelConfirmDialog(callback: (value){
                      String str = logic.read('temp_text', defaultValue: '');
                      if(str.isNotEmpty) {
                        logic.showLoading();
                        logic.deleteAccount(str).then((rsp) {
                          logic.cancelLoading();

                          var code = rsp[NetConst.keyCode];
                          var message = rsp[NetConst.keyMessage];

                          if(code == NetConst.deleteAccountSuccess) {
                            logic.onDeleteAccount();
                            logic.showTips('', LocaleKeys.user_delete_successful.ts);
                            Get.until((route) => Get.currentRoute=='/main');
                          }
                          else if (code == NetConst.checkPwdFailed) {
                            logic.showTips('', message);
                          }
                          else {
                            logic.showTips('', HttpConst.getCodeToString(code));
                          }
                        });
                      } else {
                        logic.showTips('', LocaleKeys.login_input_password.ts);
                      }
                    });
                  });
                },
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(ColorConst.activeColor),
                    shape: MaterialStateProperty.all(RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    ))
                ),
                child: Text(LocaleKeys.reset_ok.ts, style: TextStyle(fontSize: 48.sp, color: Colors.white, ),),
              ),),
          ],
        ),
      ),
    );
  }

}